package com.liqi.web.controller.system;

import com.liqi.common.annotation.Log;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.AjaxResult;
import com.liqi.common.core.page.TableDataInfo;
import com.liqi.common.enums.BusinessType;
import com.liqi.common.utils.poi.ExcelUtil;
import com.liqi.system.domain.SysRegion;
import com.liqi.system.domain.vo.SysRegionVo;
import com.liqi.system.service.ISysRegionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 行政区域管理Controller
 *
 * @author ymh
 * @date 2022-02-21
 */
@Api(tags = "区域管理接口")
@RestController
@RequestMapping("/system/region")
public class SysRegionController extends BaseController {
    @Autowired
    private ISysRegionService sysRegionService;

    /**
     * 查询行政区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRegion sysRegion) {
        startPage();

        if(sysRegion.getName()!=null||sysRegion.getCode()!=null){
            sysRegion.setParentId(null);
        }

        if("".equals(sysRegion.getName())&&"".equals(sysRegion.getCode())){
            sysRegion.setName(null);
            sysRegion.setCode(null);
            sysRegion.setParentId((long) 0);
        }
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegion);
        return getDataTable(list);
    }

    /**
     * 获取设备投放城市
     */
    /*@PreAuthorize("@ss.hasPermi('system:region:getregion')")*/
    @GetMapping(value = "/getregion")
    public TableDataInfo getregion(@RequestParam(value="id", required=true)Long id)
    {
        startPage();
        List<SysRegion> list = sysRegionService.selectSysRegionListByParentId(id);
        return getDataTable(list);
    }


    /**
     * 导出行政区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:export')")
    @Log(title = "行政区域管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRegion sysRegion) {
        if(sysRegion.getName()!=null||sysRegion.getCode()!=null){
            sysRegion.setParentId(null);
        }
        if("".equals(sysRegion.getName())){
            sysRegion.setName(null);
            sysRegion.setCode(null);
            sysRegion.setParentId((long) 0);
        }

        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegion);
        ExcelUtil<SysRegion> util = new ExcelUtil<SysRegion>(SysRegion.class);
        util.exportExcel(response, list, "行政区域管理数据");
    }

    /**
     * 获取行政区域管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysRegionService.selectSysRegionById(id));
    }

    /**
     * 新增行政区域管理
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @Log(title = "行政区域管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRegion sysRegion) {
        String id = String.valueOf(sysRegion.getParentId());
        SysRegion sysRegionParent = sysRegionService.selectSysRegionById(id);
        String code  = String.valueOf(sysRegion.getCode());



        if(code.length()!=6&&code.length()!=12){
            return AjaxResult.error("请输入6位或者12位行政区域代码");
        }

        if(id.equals("0")){
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            sysRegion.setAncestors("0");

        }
        else if (sysRegionParent.getParentId()==0){
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();


            if(String.valueOf(sysRegionParent.getCode()).startsWith("11")||String.valueOf(sysRegionParent.getCode()).startsWith("12")||String.valueOf(sysRegionParent.getCode()).startsWith("31")||String.valueOf(sysRegionParent.getCode()).equals("50")){
                sysRegion.setAncestors(ancestors + "," + id);
            }else {
                sysRegion.setAncestors(ancestors + "," + id);
            }
        }

        else if(!String.valueOf(sysRegionParent.getCode()).startsWith("00",2)&&String.valueOf(sysRegionParent.getCode()).startsWith("00",4)&&String.valueOf(sysRegionParent.getCode()).length()==6)
        {
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();
            sysRegion.setAncestors(ancestors+","+id);
        }
        else if(!String.valueOf(sysRegionParent.getCode()).startsWith("00",2)&&!String.valueOf(sysRegionParent.getCode()).startsWith("00",4)&&String.valueOf(sysRegionParent.getCode()).length()==6){
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();
            sysRegion.setAncestors(ancestors+","+id);
        } else if(!String.valueOf(sysRegionParent.getCode()).startsWith("00",2)&&!String.valueOf(sysRegionParent.getCode()).startsWith("00",4)&&String.valueOf(sysRegionParent.getCode()).length()==12){
            if(code.length()<12){
                return AjaxResult.error("请输入12位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();
            sysRegion.setAncestors(ancestors+","+id);
        }
            return toAjax(sysRegionService.insertSysRegion(sysRegion));
    }

    /**
     * 修改行政区域管理
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @Log(title = "行政区域管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegion sysRegion) {
        String id = String.valueOf(sysRegion.getParentId());
        SysRegion sysRegionParent = sysRegionService.selectSysRegionById(id);
        String code  = String.valueOf(sysRegion.getCode());
        if(code.length()!=6&&code.length()!=12){
            return AjaxResult.error("请输入6位或者12位行政区域代码");
        }
        if(id.equals("0")){
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            sysRegion.setAncestors("0");
        }
        else if (sysRegionParent.getParentId()==0){
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();
            if(String.valueOf(sysRegionParent.getCode()).startsWith("11")||String.valueOf(sysRegionParent.getCode()).startsWith("12")||String.valueOf(sysRegionParent.getCode()).startsWith("31")||String.valueOf(sysRegionParent.getCode()).equals("50")){
                sysRegion.setAncestors(ancestors + "," + id);
            }else {

                sysRegion.setAncestors(ancestors + "," + id);
            }
        }
        else if(!String.valueOf(sysRegionParent.getCode()).startsWith("00",2)&&String.valueOf(sysRegionParent.getCode()).startsWith("00",4)&&String.valueOf(sysRegionParent.getCode()).length()==6)
        {
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();

            sysRegion.setAncestors(ancestors+","+id);
        }
        else if(!String.valueOf(sysRegionParent.getCode()).startsWith("00",2)&&!String.valueOf(sysRegionParent.getCode()).startsWith("00",4)&&String.valueOf(sysRegionParent.getCode()).length()==6){
            if(code.length()!=6){
                return AjaxResult.error("请输入6位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();

            sysRegion.setAncestors(ancestors+","+id);
        } else if(!String.valueOf(sysRegionParent.getCode()).startsWith("00",2)&&!String.valueOf(sysRegionParent.getCode()).startsWith("00",4)&&String.valueOf(sysRegionParent.getCode()).length()==12){
            if(code.length()<12){
                return AjaxResult.error("请输入12位行政区域代码");
            }
            String ancestors = sysRegionParent.getAncestors();
            sysRegion.setAncestors(ancestors+","+id);
        }

        return toAjax(sysRegionService.updateSysRegion(sysRegion));
    }

    /**
     * 删除行政区域管理
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
    @Log(title = "行政区域管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysRegionService.deleteSysRegionByIds(ids));
    }

    /**
     * 级联菜单显示
     */
    @ApiOperation("查询区域")
    @GetMapping("/data")
    public List<SysRegionVo> data(SysRegionVo sysRegionVo) {
        List<SysRegionVo> data = sysRegionService.selectSysRegionData(sysRegionVo);
        return data;
    }
    /**
     * 返回前两级显示
     */
    @GetMapping("/level2")
    public List<SysRegionVo> selectSysRegionLevel2() {

        List<SysRegionVo> level2 = sysRegionService.selectSysRegionLevel2();
        return level2;
    }
}
